package au.edu.unsw.infs3634.gamifiedlearning.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import au.edu.unsw.infs3634.gamifiedlearning.R;
import au.edu.unsw.infs3634.gamifiedlearning.model.SendModel;

public class SendAdapter extends RecyclerView.Adapter<SendAdapter.SendHolder> {

    private List<SendModel> sendModelList;

    private View view;

    public SendAdapter(List<SendModel> sendModelList) {
        this.sendModelList = sendModelList;
    }

    @NonNull
    @Override
    public SendAdapter.SendHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.send_item, parent, false);
        SendHolder sendHolder = new SendHolder(view);
        return sendHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull SendAdapter.SendHolder holder, int position) {
        SendModel sendModel = sendModelList.get(position);
        holder.name.setText(sendModel.getName());
        holder.radioButton.setChecked(false);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RadioButton radioButton  = view.findViewById(R.id.index);
                radioButton.setChecked(true);
                onItemListener.onClick(sendModel);
            }
        });
    }

    @Override
    public int getItemCount() {
        return sendModelList.size();
    }


    class SendHolder extends RecyclerView.ViewHolder {

        private RadioButton radioButton;
        private TextView name;


        public SendHolder(@NonNull View itemView) {
            super(itemView);
            this.radioButton = itemView.findViewById(R.id.index);
            this.name = itemView.findViewById(R.id.name);
        }
    }

    private OnItemListener onItemListener;

    public void setOnItemListener(SendAdapter.OnItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    public interface OnItemListener {
        void onClick(SendModel sendModel);
    }
}
